package com.spstudio.iforward.end.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.faces.model.SelectItem;
import javax.inject.Inject;

import com.spstudio.iforward.core.helper.DatabaseHelper;
import com.spstudio.iforward.end.interfaces.ISelectionFetcherBeanRemote;

/**
 * Session Bean implementation class SelectionFetcherBean
 */
@Singleton
@Startup
public class SelectionFetcherBean implements ISelectionFetcherBeanRemote {

	@Inject DatabaseHelper helper;
	
	private static final String QUERY_STORY_CATEGORY_SQL = "SELECT id, category_name FROM story_category";
	private List<SelectItem> returnList = null;
	
    /**
     * Default constructor. 
     */
    public SelectionFetcherBean() {
    }
	
	@PostConstruct
	public void postConstruct(){
		timer();
	}

	@Override
	public List<SelectItem> retrieveStoryCategorySelectionList() {
		return returnList;
	}

	@Schedule(minute = "*/10", hour = "*", persistent = false)
	public void timer(){
		List<SelectItem> list = new ArrayList<SelectItem>();
		List<Object[]> result = null;
		result = helper.doQuery(QUERY_STORY_CATEGORY_SQL, null);
		if (result != null && result.size() != 0){
			for(Object[] row: result){
				list.add(new SelectItem(row[0], (String)row[1]));
			}
		}
		returnList = list;
	}
}
